package top.hidoctor.auscultation;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;


import com.tang.util.Event;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.feature.fragment.ShaichaFragment;
import top.hidoctor.auscultation.feature.fragment.device.DeviceFragment;
import top.hidoctor.auscultation.feature.fragment.mine.MineFragment;
import top.hidoctor.auscultation.feature.fragment.patient.PatientFragment;
import top.hidoctor.auscultation.feature.fragment.statistics.StatisticsFragment;
import top.hidoctor.auscultation.feature.fragment.work.WorkFragment;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private FrameLayout frameContent;
    private RadioButton rbWork, rbPatient, rbDevice, rbShaicha, rbMine;
    private RadioGroup rgGroup;
    private int[] resId = new int[]{R.id.rb_patient, R.id.rb_work, R.id.rb_device, R.id.rb_shaicha, R.id.rb_mine};
    private ArrayList<Fragment> fragments;
    private int position = 0;
    private FragmentTransaction ft;
    private Fragment CURRENT_FRAGMENT;
    private Long exitTime = 0L;
    int tab_number = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
    }

    private void initView() {
        frameContent = findViewById(R.id.frame_content);
        rgGroup = findViewById(R.id.rg_group);
        rbPatient = findViewById(R.id.rb_patient);
        rbWork = findViewById(R.id.rb_work);
        rbDevice = findViewById(R.id.rb_device);
        rbShaicha = findViewById(R.id.rb_shaicha);
        rbMine = findViewById(R.id.rb_mine);


        rbPatient.setOnClickListener(this);
        rbWork.setOnClickListener(this);
        rbDevice.setOnClickListener(this);
        rbShaicha.setOnClickListener(this);
        rbMine.setOnClickListener(this);
    }

    private void initEvent() {
        fragments = new ArrayList<>();
        fragments.add(0, new PatientFragment());
        fragments.add(1, new WorkFragment());
        fragments.add(2, new DeviceFragment());
        fragments.add(3, new ShaichaFragment());
        fragments.add(4, new MineFragment());
        rgGroup.check(resId[position]);
        switchFragment(tab_number);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_patient:
                tab_number = 0;
                switchFragment(0);
                break;
            case R.id.rb_work:
                tab_number = 1;
                switchFragment(1);
                break;
            case R.id.rb_device:
                tab_number = 2;
                switchFragment(2);
                break;
            case R.id.rb_shaicha:
                tab_number = 3;
                switchFragment(3);
                break;
            case R.id.rb_mine:
                tab_number = 4;
                switchFragment(4);
                break;
        }

    }

    private void switchFragment(int pos) {
        position = pos;
        rgGroup.check(resId[position]);
        ft = getSupportFragmentManager().beginTransaction();
        if (CURRENT_FRAGMENT != null) {
            ft.hide(CURRENT_FRAGMENT);
        }
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(fragments.get(pos).getClass().getName());
        if (fragment == null) {
            fragment = fragments.get(pos);
        }
        CURRENT_FRAGMENT = fragment;
        if (!fragment.isAdded()) {
            ft.add(R.id.frame_content, fragment, fragment.getClass().getName());
        } else {
            ft.show(fragment);
        }
        ft.commitAllowingStateLoss();
    }

    public void setInitView() {
        if (tab_number == 0) {
            rbPatient.setChecked(true);
        }
        if (tab_number == 1) {
            rbWork.setChecked(true);
        }
        if (tab_number == 2) {
            rbDevice.setChecked(true);
        }
        if (tab_number == 3) {
            rbShaicha.setChecked(true);
        }
        if (tab_number == 4) {
            rbMine.setChecked(true);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void exit() {
        if ((System.currentTimeMillis() - exitTime) > 2000) {
            showToast("再按一次退出应用");
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}